/****************************************************
* Unity版本：2022.1.21f1c1
* Version：0.1
* Company：DefaultCompany
* 日期：2023/06/20 17:09:21
*****************************************************/

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using HHHFramework.MQTT;
using MQTT;
using CPRMulti.Tools;

public class ServerUI : MonoBehaviour
{
    public InputField port,hopstIP,userName,password;
    public Button startServerBtn, stopServerBtn;
    public Text client, topic, tips;
    public Button Test;
    [SerializeField]
    MqttServer mqttServer;
    void Start()
    {
        Loom.Initialize();
        startServerBtn.onClick.AddListener(StartServerClick);
        stopServerBtn.onClick.AddListener(StopServerClick);
     
    }
    public void StartServerClick() {
        mqttServer = new MqttServer();
        mqttServer.OpenMqttServer(userName.text,password.text,int.Parse(port.text));
        mqttServer.connectClientCallBack = ConnectedFunction;
        mqttServer.messageRecive = ReciveMsgFunction;
        mqttServer.msgSubscribe = SubscribeFunction;
        Test.onClick.AddListener(TestButtonClick);
    }
    public void TestButtonClick() {
        mqttServer.ClientSubTopicsSelect();
    }
    public void StopServerClick()
    {
        mqttServer.Disconnect();
    }
    private void OnApplicationQuit()
    {
        mqttServer.Disconnect();
    }
    private void SubscribeFunction(string id,string topicStr,bool isSub) {
        topic.text += $"{topicStr}\n";
    }
    private void ReciveMsgFunction(string id,string topic,string msgStr) {
        tips.text += $"ID:{id}-topic:{topic}-Msg:{msgStr}\n";
    }
    private void ConnectedFunction(string id,bool isConnected)
    {
        client.text += $"{id}\n";
    }
}
